package com.threaddemo4;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class ThenApplyDemo1 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //thenApply:使用的是和上一个任务同一个线程
        //为什么能使用同一个线程?
        //
        CompletableFuture<String> f1 = CompletableFuture.supplyAsync(()->{
            System.out.println("A 任务 " + Thread.currentThread().getName());
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "Hello";
        });
        //将上一个任务的结果 作为参数 传入:thenApply需要等第一个任务结束
        CompletableFuture<String> f2 = f1.thenApply(r->{
            System.out.println("B任务 参数 = " + r);
            System.out.println(Thread.currentThread().getName());
            return "F2 返回值";
        });

        String ret1 = f1.get();
        String ret2 = f2.get();

        System.out.println("ret1 = " + ret1);
        System.out.println("ret2 = " + ret2);

    }
}
